<script>
import FormRender from '@/components/FreeForm/render/index.vue'
import { getBpmRule } from '@/api/bpm/rules'
import { isEmpty } from 'lodash'
import { getActiveProcessDefinitionByKey } from '@/api/bpm/definition'
import ProcessImage from '@/components/PrcoessImage/index.vue'
import { submitTask } from '@/api/business/business'
import { BtnEvents } from '@/utils/constant'

export default {
  name: 'HandleTask',
  components: { ProcessImage, FormRender },
  data() {
    return {
      ctx: {
        data: null,
        vars: null,
        formKey: null,
        businessKey: null
      },
      handlers: {
        nextNode: '',
        taskName: '',
        executeCmd: '',
        comment: '',
        backNodeCode: '',
        chooseUserId: ''
      },
      loading: false,
      definition: null,
      taskInfo: null,
      bpmRule: null,
      visible: false,
      handleVisible: false,
      nextNode: ''
    }
  },
  created() {
    this.taskInfo = this.$route.query
    this.ctx.businessKey = this.taskInfo.businessKey
    this.getProcessRule()
    this.getProcessDefinitionInfo()
  },
  methods: {
    getProcessRule() {
      const data = {
        processInstanceId: this.taskInfo.processInstanceId,
        modelKey: this.taskInfo.processDefinitionKey,
        nodeCode: this.taskInfo.taskDefinitionKey
      }
      getBpmRule(data).then(res => {
        this.bpmRule = res.data
        if ((isEmpty(this.bpmRule) || isEmpty(this.bpmRule.formCode))) {
          return this.$message.error('流程未配置页面，请配置对应的处理页面')
        }
        this.ctx.formKey = this.bpmRule.formCode
        this.taskInfo.name = this.taskInfo.name || this.bpmRule.nodeName
        this.taskInfo.taskDefinitionKey = this.taskInfo.taskDefinitionKey || this.bpmRule.nodeCode
        this.taskInfo.comment = this.bpmRule.comment || ''
        if (this.bpmRule.defaultBtnEvent && this.bpmRule.defaultBtnEvent.length > 0) {
          this.handlers.executeCmd = this.bpmRule.defaultBtnEvent
        } else if (this.bpmRule.btnEvents && this.bpmRule.btnEvents.length > 0) {
          this.handlers.executeCmd = this.bpmRule.btnEvents[0]
        }
        this.handlers.comment = this.taskInfo.comment
        this.$forceUpdate()
      })
    },
    getProcessDefinitionInfo() {
      if (!this.taskInfo.processDefinitionKey) return
      getActiveProcessDefinitionByKey(this.taskInfo.processDefinitionKey).then(res => {
        this.definition = res.data
        this.taskInfo.processDefinitionKey = this.definition.id
      })
    },
    handler() {
      this.handlers.taskName = this.taskInfo.name
      this.handleVisible = true
    },
    submit() {
      this.$refs.formRender.validate().then((instance) => {
        instance.task = this.taskInfo
        instance.process = this.taskInfo
        this.loading = true
        submitTask(instance).then(() => {
          this.$message.success('任务提交成功')
          this.$router.back()
        }).finally(() => {
          this.loading = false
        })
        console.log(instance)
      }).catch(() => {
        this.$message.error('表单校验不通过！')
      })
    },
    useHandlerEvents() {
      return BtnEvents.filter(e => this.bpmRule.btnEvents.includes(e.value))
    },
    handleBack(e) {
      this.$forceUpdate()
      const obj = this.bpmRule.backNodeList.find((item) => {
        return item.nodeCode === e
      })
      this.nextNode = obj.nodeName
    },
    handleSubmit() {
      const { executeCmd, backNodeCode, chooseUserId, comment } = this.handlers
      if (isEmpty(executeCmd)) {
        return this.$message.error('请选择处理方式')
      }
      if (executeCmd === 'back' && isEmpty(backNodeCode)) {
        return this.$message.error('请选择退回节点')
      }
      if ((executeCmd === 'delegate' || executeCmd === 'assign') && isEmpty(chooseUserId)) {
        return this.$message.error('请选择处理人')
      }
      this.taskInfo.backNodeCode = backNodeCode
      this.taskInfo.comment = comment
      this.taskInfo.assignee = chooseUserId
      this.taskInfo.executeCmd = executeCmd
      this.submit()
    }
  }
}
</script>

<template>
  <el-container class="handle-task">
    <el-main v-loading="loading">
      <form-render
        ref="formRender"
        v-model="ctx.data"
        :vars.sync="ctx.vars"
        :form-code="ctx.formKey"
        :business-key="ctx.businessKey"
        :is-design="false"
      />
    </el-main>

    <template>
      <el-dialog title="查看流程图" :visible.sync="visible" width="70%">
        <process-image :process-definition-id="taskInfo.processDefinitionKey" :process-instance-id="taskInfo.processInstanceId" />
      </el-dialog>
    </template>

    <el-drawer :visible.sync="handleVisible" size="400px" title="任务处理">
      <div class="handle-task__plane" style="padding: 10px">
        <el-form label-width="100px" :model="handlers">
          <el-form-item label="节点名称">
            <span>{{ handlers.taskName }}</span>
          </el-form-item>
          <el-form-item label="下一节点">
            <span>{{ handlers.nextNode }}</span>
          </el-form-item>
          <el-form-item label="处理方式" prop="executeCmd">
            <el-radio-group v-model="handlers.executeCmd">
              <el-radio v-for="dict in useHandlerEvents()" :key="dict.value" :label="dict.value">
                {{ dict.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item v-if="handlers.executeCmd === 'back'" label="退回节点" prop="backNodeCode">
            <el-select v-model="handlers.backNodeCode" clearable style="width: 100%;" @change="handleBack($event)">
              <el-option
                v-for="(item,index) in bpmRule.backNodeList"
                :key="index"
                :label="item.nodeName"
                :value="item.nodeCode"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="处理意见">
            <el-input v-model="handlers.comment" type="textarea" :min="32" :max="32" />
          </el-form-item>
        </el-form>
        <div style="margin-top: 10px;text-align: right">
          <el-button type="primary" @click="handleSubmit()">提交</el-button>
        </div>
      </div>
    </el-drawer>

    <el-footer height="50px">
      <div class="handle-task__footer">
        <el-button size="small" icon="el-icon-back" @click="$router.back()">返回</el-button>
        <el-button size="small" icon="el-icon-s-home" @click="$router.push('/')">首页</el-button>
        <el-button size="small" icon="el-icon-picture" @click="visible = true">流程图</el-button>
        <el-button size="small" icon="el-icon-edit-outline" type="primary" @click="handler">提交</el-button>
      </div>
    </el-footer>
  </el-container>
</template>

<style scoped lang="scss">
.handle-task {
  position: absolute;
  height: 100%;
  width: 100%;
  background: #f3f3f3;
}

.el-main {
  padding: 10px;
}

.handle-task__footer {
  height: 100%;
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  border-top: 1px solid #dcdcdc;
}

.el-footer {
  padding: 0;
  background: #fff;
}

::v-deep .el-form-item {
  margin-bottom: 10px;
}

.handle-task__plane {
  ::v-deep .el-form-item__label {
    font-weight: 400 !important;
    padding: 5px;
    color: #909399;
    font-size: 13px;
  }

  ::v-deep .el-form-item {
    margin: 0;
    border-top: 1px solid #DCDFE6;
    border-right: 1px solid #DCDFE6;
    background: #fafafa;
  }

  .el-form {
    border-bottom: 1px solid #DCDFE6;
    border-left: 1px solid #DCDFE6;
  }

  ::v-deep .el-form-item__content {
    padding: 5px;
    border-left: 1px solid #DCDFE6;
    background: white;
    font-size: 13px;
  }
}

::v-deep .el-drawer__header {
  font-weight: bolder;
  padding: 10px;
  border-bottom: 1px solid #DCDFE6;
  background: #EFEFEF;
  margin-bottom: 0;
}
</style>
